﻿using SpaceNeedle.SDK.Windows.Platform.Commands;
using SpaceNeedle.SDK.Windows.Platform.Data;
using SpaceNeedle.SDK.WindowsPhone;
using SpaceNeedle.x6473.WindowsPhone.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace SpaceNeedle.x6473.WindowsPhone.ViewModels
{
    public class AuthViewModel : GlobalViewModel
    {
        public event EventHandler SignedOut = delegate { };

        public AuthViewModel() :base()
        {
            this.SignInCommand = new RelayCommand(SignIn);
            this.SignOutCommand = new RelayCommand(SignOut);
        }

        public string PageTitle
        {
            get { return this.Get<string>(); }
            set { this.Set(value); }
        }

        public string CallTitle
        {
            get { return this.Get<string>(); }
            set { this.Set(value); }
        }

        public string UserText
        {
            get { return this.Get<string>(); }
            set { this.Set(value); }
        }

        public string PasswordText
        {
            get { return this.Get<string>(); }
            set { this.Set(value); }
        }

        public bool IsWelcome
        {
            get { return this.Get<bool>(); }
            set { this.Set(value); }
        }

        public bool IsAuthenticated
        {
            get { return this.Get<bool>(); }
            set { this.Set(value); }
        }

        public ICommand SignInCommand { get; set; }
        public ICommand SignOutCommand { get; set; }

        private async void SignIn(object param)
        {
            this.IsAuthenticated = true;

            this.IsInProgress = true;

            KeyValuePair<string, string> data = (KeyValuePair<string, string>)param;

            string provider = data.Key;
            string userId = data.Value;

            var me = await Api.GetMe(userId);

            AppViewModel.Instance.Properties.SocialNetworkName = provider;
            AppViewModel.Instance.Properties.Me = me;

            if(me == null)
            {
                ////Error
                //await Deployment.Current.Dispatcher.InvokeAsync(() =>
                //{
                //    MessageBox.Show("cant get me");
                //});
            }

            await Api.SetPushNotificationChannelUri(me, AppViewModel.Instance.Properties.PushNotificationChannelUri);

            await AppViewModel.Instance.SaveAppState();

            await AppViewModel.Instance.LoadPhones();
            await AppViewModel.Instance.LoadRequests();
            await AppViewModel.Instance.LoadAppContent();

            AppServices.Current.NavigationService.NavigateDirect(ViewsUris.Home);

            this.IsAuthenticated = AppViewModel.Instance.IsAuthenticated;

            this.IsInProgress = false;
        }

        private async void SignOut(object param)
        {
            AppViewModel.Instance.Properties.SocialNetworkName = string.Empty;
            AppViewModel.Instance.Properties.Me = null;

            this.IsAuthenticated = AppViewModel.Instance.IsAuthenticated;

            this.ClearCacheCommand.Execute(null);

            await AppViewModel.Instance.SaveAppState();

            this.SignedOut(null, EventArgs.Empty);
        }
        
        protected override void Load()
        {
            this.IsAuthenticated = AppViewModel.Instance.IsAuthenticated;

            Tracking.SendView(ViewsNames.Auth);
        }
    }
}
